/* PatternForamtter.java
 * Author: liuex
 * 2007-12-16 ����12:36:09
 *
 * Note: 
 */
package org.lex.utils.logging;

import java.util.logging.Formatter;
import java.util.logging.LogRecord;

import org.lex.utils.logging.pattern.Pattern;
import org.lex.utils.logging.pattern.PatternParser;
import org.lex.utils.text.PadUtilities;


public class PatternFormatter extends Formatter {
	// public static final String DEFAULT_PATTERN =
	// "%d %t %l [%p] %c{1}.%M(): %m%n";
	public static final String DEFAULT_PATTERN = "[%t] [%p] %c{1}.%M(): %m%n";

	private Pattern[] patterns = new Pattern[0];

	public PatternFormatter() {
		this(DEFAULT_PATTERN);
	}

	public PatternFormatter(String pattern) {
		this.patterns = new PatternParser(pattern).parse();
	}

	@Override
	public String format(LogRecord record) {
		Pattern p = null;
		StringBuilder buf = new StringBuilder();
		for (int i = 0; i < patterns.length; i++) {
			p = patterns[i];
			PadUtilities.pad(p.format(record), buf, p.isLeftToRight(), p.getMinimalLength(), p.getMaximalLength());
		}
		return buf.toString();
	}
}
